/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author Klaas Dons
 */
public class Main extends MIDlet implements CommandListener {
    private GetPlayer getPlayer;
    private SetPlayer setPlayer;
    
    private GetHighScores getHighScores;
    private SetHighScore setHighScore;
    
    //private HighScoreManager hm;
    private MarioCanvas gameCanvas = null; 
    private Thread t = null;
    private Display display;
    
    private List menu;
    private Command exitCommand, okCommand;
    
    private List lstHighScores;
    
    private Form frmSetPlayer;
    private StringItem lblLabelPlayer;
    private TextField txtPlayerName;
    private Command cancelCommand, saveCommand, backCommand;
    private int highScore = 0;
    
    public Main(){
        this.display = Display.getDisplay(this);
        //this.hm = new HighScoreManager();
        
        String[] listElements = { "Speel", "Topscores", "Spelernaam" };
        this.menu = new List("", List.IMPLICIT, listElements, null);
        
        this.exitCommand = new Command("Afsluiten", Command.EXIT, 1);
        this.okCommand = new Command("Selecteer", Command.SCREEN, 0);
        
        this.menu.addCommand(this.exitCommand);
        this.menu.addCommand(this.okCommand);
        this.menu.setCommandListener(this);
        
        //Het scherm om de spelersnaam in te voeren.
        this.frmSetPlayer = new Form("Spelersnaam");
        this.lblLabelPlayer = new StringItem("", "Geef u spelersnaam op. Deze wordt gebruikt bij het uploaden van uw score.");
        this.txtPlayerName = new TextField("Spelersnaam", "", 20, TextField.ANY);
        
        this.cancelCommand = new Command("Annuleren", Command.BACK, 1);
        this.saveCommand = new Command("Opslaan", Command.OK, 0);
        
        this.backCommand = new Command("Terug", Command.BACK, 0);
        
        this.frmSetPlayer.append(this.lblLabelPlayer);
        this.frmSetPlayer.append(this.txtPlayerName);
        this.frmSetPlayer.addCommand(this.cancelCommand);
        this.frmSetPlayer.addCommand(this.saveCommand);
        this.frmSetPlayer.setCommandListener(this);
        setFormPlayerName();
    }
    
    public void setHightScore(int score) {
        this.getPlayer = new GetPlayer(txtPlayerName);
        this.highScore = score;
        setHighScore = new SetHighScore(this.txtPlayerName.getString(), this.highScore);
        this.t = new Thread(this.setHighScore);
        this.t.start();
        showMenu();
    }
    
    public void startApp() {
        this.showMenu();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
    
    public void commandAction(Command c, Displayable s){
        if (c == this.okCommand || c == List.SELECT_COMMAND){
            int index = this.menu.getSelectedIndex();
            
            if (index == 0){
                this.showGame();
            } else if (index == 1) {
                this.prepareHighScore();
            } else if (index == 2) {
                this.showSetPlayer();
            } else {           
                Alert alert = new Alert("Keuze", this.menu.getString(index), null, AlertType.INFO);
                Display.getDisplay(this).setCurrent(alert, this.menu);
            }
        } else if (c == this.saveCommand){
            this.setPlayer = new SetPlayer(this.txtPlayerName.getString());
            this.t = new Thread(this.setPlayer);
            t.start();
            
            this.showMenu();
        } else if (c == this.cancelCommand || c == this.backCommand){
            this.showMenu();
        } else if (c == this.exitCommand) {
            notifyDestroyed();
        }
    }
    
    public void showMenu(){
        gameCanvas = null;
        t = null;
        Display.getDisplay(this).setCurrent(this.menu);
    }
    
    private void showGame(){
        gameCanvas = new MarioCanvas(this);
        t = new Thread(gameCanvas);
        t.start();
        Display.getDisplay(this).setCurrent(gameCanvas);
    }
    
    public void prepareHighScore() {
        this.getHighScores = new GetHighScores(this.lstHighScores, this);
        this.t = new Thread(this.getHighScores);
        this.t.start();
    }
    
    public void showHighScore(List list) {
        this.lstHighScores = list;
        this.lstHighScores.addCommand(this.backCommand);
        this.lstHighScores.setCommandListener(this);
        Display.getDisplay(this).setCurrent(this.lstHighScores);
              
    }
    
    public void setFormPlayerName() {
        this.getPlayer = new GetPlayer(txtPlayerName);
        this.t = new Thread(this.getPlayer);
        this.t.start(); 
    }
    
    public void showSetPlayer(){
        setFormPlayerName();
        Display.getDisplay(this).setCurrent(this.frmSetPlayer);
    }
    
}


